package com.example.socialnews;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.hhdd.messi.Naver;
import com.hhdd.messi.Daum;
import com.hhdd.messi.daum.socialpick.SocialPickObject;
import com.hhdd.messi.event.NaverEventListener;
import com.hhdd.messi.event.DaumEventListener;
import com.hhdd.messi.naver.object.search.NewsObject;

public class MainActivity extends Activity implements NaverEventListener.OnNewsListener{
	
	private Daum daum_api;
	private Naver naver_api;
	private ArrayList<SocialPickObject> rank_lists;
	private int rank_point = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		
		
		daum_api = new Daum();
		naver_api = new Naver();
		naver_api.setSearchKey("5be33141055ee2769497d4b01b0da9a8");
		naver_api.setNewsListener(this);
		
		daum_api.setSocialPickListener(new DaumEventListener.OnSocialpickListener() {
			
			public void onResult(ArrayList<SocialPickObject> arg0) {
				rank_lists = arg0;
				rank_point = 0;
				donewsSearch();
			}
			
			public void onFaultResult(int arg0, String arg1) {
				
			}
		});
		daum_api.SocialPick(10);
		
		Button bt1 = (Button) findViewById(R.id.btn1);
		Button bt2 = (Button) findViewById(R.id.btn2);
		bt1.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				rank_point--;
				if(rank_point < 0)
					rank_point = 9;
				donewsSearch();
			}
		});
		
		bt2.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				rank_point++;				
				if(rank_point > 9)
					rank_point = 0;
				donewsSearch();				
			}			
		});
		
	}
	
	private void donewsSearch()
	{
		TextView tv = (TextView) findViewById(R.id.tv1);
		SocialPickObject obj = rank_lists.get(rank_point);
		tv.setText(obj.getKeyword());
		naver_api.NewsSearch(obj.getKeyword());		
	}

	public void onFaultResult(int arg0, String arg1) {
		// TODO Auto-generated method stub
		
	}

	public void onResult(ArrayList<NewsObject> arg0) {
		// TODO Auto-generated method stub
		ListView listView = (ListView) findViewById(R.id.lv1);
		ListAdapter b1_adapter = new ListAdapter(this, R.layout.item_row, arg0);
		listView.setAdapter(b1_adapter);
	}
	
	public class ListAdapter extends ArrayAdapter<NewsObject>{
    	public ArrayList<NewsObject> items;
    	
    	public ListAdapter(Context context, int textViewResourceId, ArrayList<NewsObject> items) {
            super(context, textViewResourceId, items);
    		this.items = items;
    	}
    	
    	@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.item_row, null);
            }
            
            NewsObject Info = items.get(position);
            
            if (Info != null) {
            	TextView tv1 = (TextView) v.findViewById(R.id.item_tv1);
            	TextView tv2 = (TextView) v.findViewById(R.id.item_tv2);
            	tv1.setText(Info.getTitle());
            	tv2.setText(Info.getDescription());
            }
            return v;
        }
    	
    }
	

}
